
package edu.moravian.managers;

import edu.moravian.entity.Enemy;
import edu.moravian.grid.Grid;
import edu.moravian.math.Point2D;
import edu.moravian.math.Rand;
import edu.moravian.math.Vector2D;
import edu.moravian.path.Path;
import edu.moravian.path.PathGenerator;
import edu.moravian.world.WorldDescriptor;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ronaghan
 */
public class WaveGenerator
{
    private static final double enemySpeed = 60;
    private static final double enemyRadius = 8.0;
    private static final double enemyHealth = 1.0;
    private static final double enemyMass = 1.0;
    
    private static final String BASE_PATH = "src/main/resources/waves";
    private int numWaves;
    private int curWave;
    
    private WorldDescriptor world;
    
    private Grid grid;
    
    public WaveGenerator(WorldDescriptor world, Grid grid)
    {
        this.grid = grid;
        numWaves = 0;
        File baseDir = new File(BASE_PATH);
        
        for(File file: baseDir.listFiles())
        {
            if(file.canRead() && file.isFile() && file.getName().contains("wave"))
            {
                numWaves++;
            }
        }
       
        curWave = 0;
        this.world = world;
    }
    
    public Wave getNextWave()
    {
        if(hasNextWave() == false)
        {
            throw new IllegalStateException("No waves left");
        }
        
        Wave newWave = loadWave(curWave);
        
        curWave++;
        return newWave;
    }
    
    public boolean hasNextWave()
    {
        if(curWave + 1 <= numWaves)
        {
            return true;
        }
        return false;
        
    }
    
    public Wave loadWave(int curWave)
    {
        String wave = "wave"+curWave+".ini";
        File file = new File(BASE_PATH, wave);
        
        Properties properties = new Properties();
        try
        {
            properties.load(new FileReader(file));
        }
        catch (IOException ex)
        {
            System.out.println("Can't load wave " + wave);
        }
        
        
        int numEnemies = Integer.parseInt(properties.getProperty("numEnemies"));
        double delayBetweenSend = Double.parseDouble(properties.getProperty("delayBetweenSend"));
        
        List<Enemy> enemies = new ArrayList<Enemy>();
        
        
        
        
        
        
        for(int i=0; i<numEnemies; i++)
        {
            
            PathGenerator pathgen = new PathGenerator(world, grid);
            Path path = pathgen.getPath();
            
            Point2D startPoint2D = new Point2D(path.getStartPoint().getX() + Rand.inRange(-4.0, 4.0), 
                    path.getStartPoint().getY() + Rand.inRange(-4.0, 4.0));
            
            
            Enemy enemy = new Enemy(startPoint2D, new Vector2D(), enemySpeed, enemyMass, enemyRadius, enemyHealth, "enemy1");
            enemy.setPath(path);
            
            enemies.add(enemy);
        }
        
        Wave ret = new Wave(enemies, delayBetweenSend);
        
        return ret;
    }
    
    
    
    public class Wave
    {
        
        private List<Enemy> enemies;
        private double sendDelay;

        public Wave(List<Enemy> enemies, double sendDelay)
        {
            this.enemies = enemies;
            this.sendDelay = sendDelay;
        }

        public List<Enemy> getEnemies()
        {
            return enemies;
        }

        public double getSendDelay()
        {
            return sendDelay;
        }
    }
}
